Conditions | 1 |
Paths | 576 |
Total Lines | 243 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | /** |
||
58 | objects.each(function collapsible(index) { |
||
59 | var object = $(this), |
||
60 | storage = settings.storage + '.' + index + '.collapsed'; |
||
61 | |||
62 | var getDuration = function (duration) { |
||
63 | return $.isNumeric(duration) ? duration : settings.delay; |
||
64 | }; |
||
65 | |||
66 | /*--------------------------------------------------------------------- |
||
67 | Events |
||
68 | ---------------------------------------------------------------------*/ |
||
69 | |||
70 | var collapseItem = function collapse(item, duration) { |
||
71 | var heightMin = 0; |
||
72 | |||
73 | // Customization point |
||
74 | item.trigger('collapsebefore.collapsible', settings); |
||
75 | |||
76 | heightMin = item.data('heightMin'); |
||
77 | |||
78 | // Check duration |
||
79 | if(duration !== 0) { |
||
80 | item.addClass('js-animate'); |
||
81 | item.trigger('collapsestart.collapsible'); |
||
82 | } |
||
83 | |||
84 | // Collapse item |
||
85 | item.addClass('collapsed'); |
||
86 | item.css('max-height', heightMin); |
||
87 | |||
88 | if(duration !== 0) { |
||
89 | setTimeout(function() { |
||
90 | item.trigger('animationend.collapsible'); |
||
91 | item.trigger('animationend.duplicator'); |
||
92 | }, duration); |
||
93 | } |
||
94 | }; |
||
95 | |||
96 | // Collapse item |
||
97 | object.on('collapse.collapsible', settings.items, function collapse(event, duration) { |
||
98 | var item = $(this); |
||
99 | collapseItem(item, getDuration(duration)); |
||
100 | }); |
||
101 | |||
102 | // Collapse all items |
||
103 | object.on('collapseall.collapsible', function collapseAll(event) { |
||
|
|||
104 | var items = object.find(settings.items + ':not(.collapsed)'), |
||
105 | visibles = Symphony.Utilities.inSight(items), |
||
106 | invisibles = $(), |
||
107 | scrollTop = $(window).scrollTop(), |
||
108 | visibleIndex = visibles.eq(0).index(), |
||
109 | visibleCollapsedHeight = 0; |
||
110 | |||
111 | // Find items that will be visible after collapse |
||
112 | while (visibleIndex < items.length && visibleCollapsedHeight < window.innerHeight) { |
||
113 | var currentItem = items.eq(visibleIndex); |
||
114 | visibles = visibles.add(currentItem); |
||
115 | visibleCollapsedHeight += currentItem.data('heightMin'); |
||
116 | visibleIndex++; |
||
117 | } |
||
118 | visibles.each(function () { collapseItem($(this), settings.delay); }); |
||
119 | |||
120 | setTimeout(function collapseAllInvisibleEnd() { |
||
121 | var first = visibles.eq(0); |
||
122 | var firstOffset = !first.length ? 0 : first.offset().top; |
||
123 | // update invisible accordingly |
||
124 | invisibles = items.not(visibles); |
||
125 | invisibles.each(function () { collapseItem($(this), 0); }); |
||
126 | if (firstOffset > 0 && scrollTop > object.offset().top) { |
||
127 | // scroll back to where we were, |
||
128 | // which is last scroll position + delta of first visible item |
||
129 | $(window).scrollTop(scrollTop + (first.offset().top - firstOffset)); |
||
130 | } |
||
131 | invisibles.trigger('animationend.collapsible'); |
||
132 | }, settings.delay + 100); |
||
133 | }); |
||
134 | |||
135 | // Expand item |
||
136 | var expandItem = function (item, duration) { |
||
137 | var heightMax = 0; |
||
138 | |||
139 | // Customization point |
||
140 | item.trigger('expandbefore.collapsible', settings); |
||
141 | |||
142 | heightMax = item.data('heightMax'); |
||
143 | |||
144 | // Check duration |
||
145 | if(duration !== 0) { |
||
146 | item.addClass('js-animate'); |
||
147 | item.trigger('expandstart.collapsible'); |
||
148 | } |
||
149 | |||
150 | // Collapse item |
||
151 | item.removeClass('collapsed'); |
||
152 | item.css('max-height', heightMax); |
||
153 | |||
154 | if(duration !== 0) { |
||
155 | setTimeout(function() { |
||
156 | item.trigger('animationend.collapsible'); |
||
157 | }, duration); |
||
158 | } |
||
159 | }; |
||
160 | |||
161 | object.on('expand.collapsible', settings.items, function expand(event, duration) { |
||
162 | var item = $(this); |
||
163 | expandItem(item, getDuration(duration)); |
||
164 | }); |
||
165 | |||
166 | // Expand all items |
||
167 | object.on('expandall.collapsible', function expandAll(event) { |
||
168 | var items = object.find(settings.items + '.collapsed'), |
||
169 | visibles = Symphony.Utilities.inSight(items).filter('*:lt(4)'), |
||
170 | invisibles = items.not(visibles), |
||
171 | scrollTop = $(window).scrollTop(); |
||
172 | |||
173 | visibles.addClass('js-animate-all'); // prevent focus |
||
174 | visibles.each(function () { expandItem($(this), settings.delay); }); |
||
175 | setTimeout(function expandAllInvisible() { |
||
176 | var first = visibles.eq(0); |
||
177 | var firstOffset = !first.length ? 0 : first.offset().top; |
||
178 | invisibles.addClass('js-animate-all'); // prevent focus |
||
179 | invisibles.each(function () { expandItem($(this), 0); }); |
||
180 | invisibles.trigger('animationend.collapsible'); |
||
181 | // if we are past the first item |
||
182 | if (firstOffset > 0 && scrollTop > object.offset().top) { |
||
183 | // scroll back to where we were, |
||
184 | // which is last scroll position + delta of first visible item |
||
185 | $(window).scrollTop(scrollTop + (first.offset().top - firstOffset)); |
||
186 | } |
||
187 | }, settings.delay + 100); |
||
188 | }); |
||
189 | |||
190 | // Finish animations |
||
191 | object.on('animationend.collapsible', settings.items, function finish(event) { |
||
192 | var item = $(this); |
||
193 | |||
194 | // Trigger events |
||
195 | if(item.is('.collapsed')) { |
||
196 | item.trigger('collapsestop.collapsible'); |
||
197 | } |
||
198 | else { |
||
199 | item.trigger('expandstop.collapsible'); |
||
200 | } |
||
201 | |||
202 | // clean up |
||
203 | item.removeClass('js-animate js-animate-all'); |
||
204 | }); |
||
205 | |||
206 | // Toggle single item |
||
207 | object.on('click.collapsible', settings.handles, function toggle(event) { |
||
208 | var handle = $(this), |
||
209 | item = handle.closest(settings.items); |
||
210 | |||
211 | if(!handle.is(settings.ignore) && !$(event.target).is(settings.ignore) && !item.is('.locked')) { |
||
212 | |||
213 | // Expand |
||
214 | if(item.is('.collapsed')) { |
||
215 | expandItem(item, settings.delay); |
||
216 | } |
||
217 | |||
218 | // Collapse |
||
219 | else { |
||
220 | collapseItem(item, settings.delay); |
||
221 | } |
||
222 | } |
||
223 | }); |
||
224 | |||
225 | // Save states |
||
226 | var saveTimer = 0; |
||
227 | object.on('collapsestop.collapsible expandstop.collapsible store.collapsible', settings.items, function saveState(event) { |
||
228 | if(settings.save_state === true && Symphony.Support.localStorage === true) { |
||
229 | // save it to local storage, delayed, once |
||
230 | clearTimeout(saveTimer); |
||
231 | saveTimer = setTimeout(function () { |
||
232 | var collapsed = object.find(settings.items).map(function(index) { |
||
233 | if($(this).is('.collapsed')) { |
||
234 | return index; |
||
235 | }; |
||
236 | }).get().join(','); |
||
237 | |||
238 | save(storage, collapsed); |
||
239 | }, settings.delay); |
||
240 | } |
||
241 | }); |
||
242 | |||
243 | // Restore states |
||
244 | object.on('restore.collapsible', function restoreState(event) { |
||
245 | if(settings.save_state === true && Symphony.Support.localStorage === true && window.localStorage[storage]) { |
||
246 | $.each(window.localStorage[storage].split(','), function(index, value) { |
||
247 | var collapsed = object.find(settings.items).eq(value); |
||
248 | if(collapsed.has('.invalid').length == 0) { |
||
249 | collapseItem(collapsed, 0); |
||
250 | } |
||
251 | }); |
||
252 | } |
||
253 | }); |
||
254 | |||
255 | // Refresh state storage after ordering |
||
256 | object.on('orderstop.orderable', function refreshOrderedState(event) { |
||
257 | object.find(settings.items).trigger('store.collapsible'); |
||
258 | }); |
||
259 | |||
260 | // Refresh state storage after deleting and instance |
||
261 | object.on('destructstop.duplicator', settings.items, function refreshState() { |
||
262 | $(this).trigger('store.collapsible'); |
||
263 | }); |
||
264 | |||
265 | // Update sizes |
||
266 | object.on('updatesize.collapsible', settings.items, function updateSizes() { |
||
267 | var item = $(this), |
||
268 | min = item.find(settings.handles).outerHeight(true), |
||
269 | max = min + item.find(settings.content).outerHeight(true); |
||
270 | |||
271 | item.data('heightMin', min); |
||
272 | item.data('heightMax', max); |
||
273 | }); |
||
274 | |||
275 | // Set sizes |
||
276 | object.on('setsize.collapsible', settings.items, function setSizes() { |
||
277 | var item = $(this); |
||
278 | var heightMin = item.data('heightMin'); |
||
279 | var heightMax = item.data('heightMax'); |
||
280 | item.css({ |
||
281 | 'min-height': heightMin, |
||
282 | 'max-height': heightMax |
||
283 | }); |
||
284 | }); |
||
285 | |||
286 | /*--------------------------------------------------------------------- |
||
287 | Initialisation |
||
288 | ---------------------------------------------------------------------*/ |
||
289 | |||
290 | // Prepare interface |
||
291 | object.addClass('collapsible').find(settings.items).each(function() { |
||
292 | var item = $(this); |
||
293 | item.addClass('instance'); |
||
294 | item.trigger('updatesize.collapsible'); |
||
295 | item.trigger('setsize.collapsible'); |
||
296 | }); |
||
297 | |||
298 | // Restore states |
||
299 | object.trigger('restore.collapsible'); |
||
300 | }); |
||
301 | |||
308 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.